package com.example.yunJie.digitalSentry.control;

import cn.afterturn.easypoi.excel.imports.sax.SaxReadExcel;
import com.example.yunJie.digitalSentry.entity.*;
import com.example.yunJie.digitalSentry.service.*;
import com.example.yunJie.digitalSentry.util.FileUtil;
import com.example.yunJie.digitalSentry.util.ListPageUtils;
import com.example.yunJie.digitalSentry.util.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/Personal")
public class PersonalControl {
    @Autowired
    private PersonalService personalService;
    @Autowired
    private MarketService marketService;
    @Autowired
    private PostService postService;
    @Autowired
    private FaceDownService faceDownService;
    @Autowired
    private EquipmentService equipmentService;


    @PostMapping("/add")
    public String  insert(String personalName, String personalCardNumber ,String departmentId, String postName,String phoneNumber, String marketName, String role_id, @RequestParam("personalPhoto") MultipartFile personalPhoto){
        String fileName = personalPhoto.getOriginalFilename();
        System.out.println("==================>>>>"+fileName);
//        String fl = fileName+".jpg";
        //设置文件上传路径
        StringBuilder filePath = new StringBuilder("C:\\checkImg\\照片-130人\\");
        if (marketName.equals("三元河市场")){
            filePath.append("三元河-32/");
        }else if (marketName.equals("城东市场")){
            filePath.append("城东-9/");
        }else if (marketName.equals("庆华市场")){
            filePath.append("庆华-8/");
        }else if (marketName.equals("万寿市场")){
            filePath.append("万寿-6/");
        }else if (marketName.equals("界泾港市场")){
            filePath.append("界泾港-14/");
        }else if (marketName.equals("八字桥市场")){
            filePath.append("八字桥-16/");
        }else if (marketName.equals("朱家角市场")){
            filePath.append("朱家角-16/");
        }else if (marketName.equals("练塘市场")){
            filePath.append("练塘-11/");
        }else if (marketName.equals("市场本部")){
            filePath.append("本部-18/");
        }else {
            filePath.append(marketName);
            filePath.append("/");
        }
        filePath.toString().replace("\\","/");
        try {
            FileUtil.uploadFile(personalPhoto.getBytes(), filePath.toString(), fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        MarketEntity idByMarketName = marketService.findIdByMarketName(marketName);
        PostEntity idByPostName = postService.findIdByPostName(postName);
        PersonalEntity personalEntity = new PersonalEntity();
        personalEntity.setMarketId(String.valueOf(idByMarketName.getId()));
        personalEntity.setPostId(String.valueOf(idByPostName.getId()));
        personalEntity.setPersonalPhoto(filePath.toString()+fileName);
        personalEntity.setPersonalCardNumber(personalCardNumber);
        personalEntity.setPersonalName(personalName);
        personalEntity.setRole_id(role_id);
        personalEntity.setDepartmentId(departmentId);
        personalEntity.setPhoneNumber(phoneNumber);
        System.out.println(personalEntity);
        if (personalService.findByPersonalName(personalName) == null){
            personalService.insert(personalEntity);
        }

        FaceDownEntity faceDownEntity = new FaceDownEntity();
        EquipmentEntity equipmentEntity = new EquipmentEntity();
        equipmentEntity.setMarketName(marketName);
        List<EquipmentEntity> all = equipmentService.findAll(equipmentEntity);
        StringBuilder sb = new StringBuilder();
        for (EquipmentEntity equipmentEntity1 : all){
            sb.append(equipmentEntity1.getEquipmentNumber());
            sb.append(";");
        }
        faceDownEntity.setDevSno(sb.toString());
        faceDownEntity.setPersonNameList(personalName);



        String id = personalService.findByPersonalName(personalName).getId();
        faceDownEntity.setTemplateImgUrlList(filePath.toString()+fileName);
        faceDownEntity.setIdCardList(personalCardNumber);
        faceDownEntity.setPidList(id);
        faceDownService.insert(faceDownEntity);

        return (filePath.toString()+fileName).replace("C:", "http://101.89.133.43:89");
    }

    @PostMapping("/update")
    public String update(String id, String personalName,String personalCardNumber,String phoneNumber, String departmentId,String postName,String marketName,String role_id,@RequestParam("personalPhoto") MultipartFile personalPhoto){
        String fileName = personalPhoto.getOriginalFilename();
        System.out.println("==================>>>>"+fileName);
//        String fl = fileName+".jpg";
        //设置文件上传路径
        StringBuilder filePath = new StringBuilder("C:\\checkImg\\照片-130人\\");
        if (marketName.equals("三元河市场")){
            filePath.append("三元河-32/");
        }else if (marketName.equals("城东市场")){
            filePath.append("城东-9/");
        }else if (marketName.equals("庆华市场")){
            filePath.append("庆华-8/");
        }else if (marketName.equals("万寿市场")){
            filePath.append("万寿-6/");
        }else if (marketName.equals("界泾港市场")){
            filePath.append("界泾港-14/");
        }else if (marketName.equals("八字桥市场")){
            filePath.append("八字桥-16/");
        }else if (marketName.equals("朱家角市场")){
            filePath.append("朱家角-16/");
        }else if (marketName.equals("练塘市场")){
            filePath.append("练塘-11/");
        }else if (marketName.equals("市场本部")){
            filePath.append("本部-18/");
        }else {
            filePath.append(marketName);
            filePath.append("/");
        }
        filePath.toString().replace("\\","/");
        try {
            FileUtil.uploadFile(personalPhoto.getBytes(), filePath.toString(), fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        MarketEntity idByMarketName = marketService.findIdByMarketName(marketName);
        PostEntity idByPostName = postService.findIdByPostName(postName);
        PersonalEntity personalEntity = new PersonalEntity();
        personalEntity.setId(String.valueOf(id));
        personalEntity.setMarketId(String.valueOf(idByMarketName.getId()));
        personalEntity.setPostId(String.valueOf(idByPostName.getId()));
        personalEntity.setPersonalPhoto(filePath.toString()+fileName);
        personalEntity.setPersonalCardNumber(personalCardNumber);
        personalEntity.setPersonalName(personalName);
        personalEntity.setRole_id(role_id);
        personalEntity.setPhoneNumber(phoneNumber);
        personalEntity.setDepartmentId(departmentId);
        System.out.println(personalEntity);
        personalService.update(personalEntity);


        FaceDownEntity bypersonalName = faceDownService.findBypidList(id);
        bypersonalName.setPersonNameList(personalName);
        bypersonalName.setTemplateImgUrlList(filePath.toString()+fileName);
        bypersonalName.setIdCardList(personalCardNumber);
        faceDownService.update(bypersonalName);

        String sb = filePath.toString()+fileName;
        String s = sb.replace("C:", "http://101.89.133.43:89");
        String resultS = s.replace("\\", "/");
        return resultS;
    }

    @RequestMapping("/deleteById")
    public int  deleteById(int id){
        return personalService.deleteById(id);
    }

    @RequestMapping("/deleteIdList")
    public int  deleteIdList(List<Integer> id){
        return personalService.deleteIdList(id);
    }

    @RequestMapping("/findId")
    public PersonalEntity  findId(String id){
        return personalService.findId(id);
    }

    @RequestMapping("/findAll")
    public  List<PersonalEntity> findAll(PersonalEntity personalEntity, Integer pageNo, Integer pageSize){
//        System.out.println(personalEntity);
        List<PersonalEntity> all = personalService.findAll(personalEntity);
        //
        for (PersonalEntity personalEntity1 : all){
            String replace = personalEntity1.getPersonalPhoto().replace("C:", "http://101.89.133.43:89");
            personalEntity1.setPersonalPhoto(replace);
        }
        //分页后的集合
//        List page = ListPageUtils.page(all, pageNo, pageSize);
//        //封装给前端的数据
//        PageParam pageParam = new PageParam();
//        pageParam.setPageNum(pageNo);
//        pageParam.setPageSize(pageSize);
//        pageParam.setTotalSize(all.size());
//        pageParam.setList(page);
        return all;
    }

    @RequestMapping("/findByMarketName")
    public List<PersonalEntity> findByMarketName(String marketName){
        return personalService.findByMarketName(marketName);
    }



}
